package org.zl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.zl.pojo.UserInfo;
import org.zl.service.UserInfoService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserInfoController {



    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpSession session;

    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping("/u_login")
    public String index(HttpServletRequest request) throws IOException {
        String userid=request.getParameter("userid");
        System.out.println("userid:"+userid);
        String password=request.getParameter("password");
        System.out.println("password:"+password);
        UserInfo userInfo=userInfoService.checkLogin(userid,password);
        if(userInfo!=null){
            session.setAttribute("session_user",userInfo);
            return "UserInfo/admin_index";
        }else{
            session.setAttribute("msg","账户或者密码错误！");
            return "UserInfo/login";
        }
    }

    /**
     * 主页面
     * @return
     */
    @RequestMapping("/MainIndex")
    public String MainIndex() {
        return "UserInfo/admin_index";
    }

    /**
     * 退出系统
     * @return
     */
    @RequestMapping("/loginOut")
    public String test(){
        return "UserInfo/login";
    }

    /**
     * 查询所有学生信息
     * @return
     */
    @RequestMapping("/u_findAllUser")
    public String findAll(HttpServletRequest request){
        List<UserInfo> userInfos=userInfoService.findAll();
        request.setAttribute("userInfos",userInfos);
        return "UserInfo/show_alluser";
    }

    @RequestMapping("/u_password")
    public String password(HttpServletResponse response) throws IOException, ServletException {

        String action=request.getParameter("action");
        //获取当前用户信息
        UserInfo userCur=(UserInfo)request.getSession().getAttribute("session_user");
        System.out.println("action:"+action+":userCur:"+userCur);
        //当前登录的用户的输入框输入的密码
        String oldPassWord=request.getParameter("oldPassword");
        System.out.println("oldPassWord:"+oldPassWord);
        //当前登录的用户的原始密码
        String password=userCur.getStuPassword();

        if(action !=null && action.equals("preChange")){
            //跳转到密码修改页
            return "UserInfo/admin_passwordChange";
        }else if(action !=null && action.equals("ajaxOldPassword")){
            //校验用户输入的原密码是否正确
            if(!password.equals(oldPassWord)){
                response.setCharacterEncoding("utf-8");
                PrintWriter writer=response.getWriter();
                writer.print("你输入的原始密码错误：请重新输入！");
            }
        }else if(action !=null && action.equals("change")){
            if(!password.equals(oldPassWord)){
                request.setAttribute("error","修改秘密失败,你输入的原始密码错误：请重新输入！");
                //跳转到密码修改页
                return "UserInfo/admin_passwordChange";
            }else {
                //修改密码
                String newPassword=request.getParameter("newPassword");
                System.out.println("newPassword:"+newPassword);
                UserInfo user=new UserInfo();
                System.out.println("userCur.getId():"+userCur.getStuUserid());
                user.setStuPassword(newPassword);
                user.setStuUserid(userCur.getStuUserid());
                user.setStuName(userCur.getStuName());
                user.setStuAge(userCur.getStuAge());
                user.setStuLocked(userCur.getStuLocked());
                userInfoService.updatePassword(user);
                //跳转到修改密码界面
                request.setAttribute("error","修改密码成功");
                session.setAttribute("session_user",user);
                return "UserInfo/admin_passwordChange";
            }
        }
        return null;
    }
}
